package gui;

import be.VisitaProgramada;
import java.util.Vector;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import midlet.MidletCielsa;
import rms.ContrasteRMS;
import util.CommandBuilder;

/**
 *
 * @author Dikas
 */
public class FrmReporteVisitasDia extends Form implements CommandListener {

    private MidletCielsa midlet;
    private Displayable previousDisplayable;
    private Command cmdSalir;
    private Command cmdOk;
    private TextField txtVisitas;
    private ChoiceGroup cgVisitas;
    private Thread hiloInterno;
    private boolean condicion = true;
    private Vector lista;
    
    public FrmReporteVisitasDia(MidletCielsa midlet, Displayable previousDisplayable) {
        super("Visitas Programadas");
        this.midlet = midlet;
        this.previousDisplayable = previousDisplayable;
        cmdSalir = CommandBuilder.getBack();
        cmdOk = CommandBuilder.getOk();
        txtVisitas = new TextField("Constraste", "", 100, TextField.ANY);
        cgVisitas = new ChoiceGroup("Seleccione uno:", ChoiceGroup.EXCLUSIVE);
        ContrasteRMS rms = new ContrasteRMS();
        lista = new Vector();
        for (int i = 0; i < rms.size(); i++) {
            VisitaProgramada visitaProgramada = rms.getVisita(i + 1);
            if (visitaProgramada.isEstado()) {                
                cgVisitas.append((i+1) + ". " + visitaProgramada.getTipo_contraste() + "-" + visitaProgramada.getZona_abrev() + visitaProgramada.getReporte(), null);
                lista.addElement((i+1) + ". " + visitaProgramada.getTipo_contraste() + "-" + visitaProgramada.getZona_abrev() + visitaProgramada.getReporte());                
            }             
        }    
        filtrar();
        addCommand(cmdSalir);
        addCommand(cmdOk);        
        append(txtVisitas);
        append(cgVisitas);
        setCommandListener(this);
        try {
            hiloInterno = new Thread(new HiloInterno());
            hiloInterno.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdSalir) {            
            midlet.setDisplay(previousDisplayable);
        }
        if (c == cmdOk) {
            if (cgVisitas.size() > 0) { 
                String visitaseleccionada = cgVisitas.getString(cgVisitas.getSelectedIndex());
                String strIndex = "";
                for(int i=0; i< visitaseleccionada.length(); i++){
                    if(visitaseleccionada.charAt(i)!='.')
                        strIndex += ""+visitaseleccionada.charAt(i);
                    else
                        break;
                }
                int index = Integer.parseInt(strIndex);
                FrmResumenContrasteRegistrado frcr = new FrmResumenContrasteRegistrado(midlet, this, index);
                midlet.setDisplay(frcr);
            }           
            
        }        
    }

    private void filtrar() {
        ContrasteRMS rms = new ContrasteRMS();
        cgVisitas.deleteAll();
        if (txtVisitas.getString().compareTo("") == 0) {
            for (int i = 0; i < lista.size(); i++) {
                cgVisitas.append(lista.elementAt(i).toString(), null);
            }
        } else {
            for (int i = 0; i < rms.size(); i++) {
                String elemento = lista.elementAt(i).toString();
                String filtro = txtVisitas.getString().toUpperCase();
                if (elemento.toUpperCase().indexOf(filtro) != -1) {
                    cgVisitas.append(elemento, null);
                }
            }
        }
    }

    private class HiloInterno implements Runnable {

        String strCliente = txtVisitas.getString();

        public void run() {

            while (condicion) {
                try {
                    if (strCliente.compareTo(txtVisitas.getString()) != 0) {
                        filtrar();
                        strCliente = txtVisitas.getString();
                    }
                    Thread.sleep(500);
                } catch (Exception e) {
                    e.printStackTrace();
                    hiloInterno.start();
                }
            }
        }
    }
  }
